package com.tz.fruit.controller.backstage;

import com.tz.fruit.entity.*;
import com.tz.fruit.service.OrderService.OrderService;
import com.tz.fruit.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Administrator on 2017/7/19.
 * 后台订单管理的控制层
 * 采用的ajax-ajon技术进行前台页面的分页管理
 * 这个前端控制器包含了两个
 */
@Controller(value="backOrderController")
@RequestMapping("/backstage")
public class OrderController {

    @Autowired
    private OrderService orderservice;

    @RequestMapping("/inquiry")
    public  String inquiry(Model model, HttpServletRequest req, HttpServletResponse resp,String pageNow, String pageSize, String orderNo,String orderStatus, String start, String end) throws IOException {
        Set<OrderItem> items = new HashSet<>();
        //新建一个页面的对象
        PageBean<Order> pageBean =new PageBean<>();

        String []status=new String[]{"已付款","未付款","用户取消","未发货","已发货","已完成"};

        model.addAttribute("status",status);

          /*因为user暂时提供不了*/
      /*  String uid=user.getUserId();*/
        String uid="1";

        if(orderNo==null  || orderNo.trim().length()==0){
            orderNo=null;
        }

        if(pageNow==null || pageNow.trim().length()==0){
            pageNow="1";

        }
        if(pageSize==null ||pageSize.trim().length()==0){
            pageSize="2";
        }
        if(start==null || start.toString().trim().length()==0) {
            start=null;
        }if(end==null || end.toString().trim().length()==0){
            end=null;
        }
        //判断类别是否为空
        if(orderStatus==null || orderStatus.trim().length()==0){
                pageBean = orderservice.getBy(null, orderNo, DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, null);

        }else{
                pageBean = orderservice.getBy(null, orderNo,DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, Enum.valueOf(OrderStatus.class, orderStatus));
            }

        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageCount", pageBean.getPageCount());
        model.addAttribute("orderNo", orderNo);
        model.addAttribute("orderStatus",orderStatus);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("pageBean", pageBean);

       return "backstage/order/list";

    }
       /*agax*/


      @RequestMapping("/delete")
       public void delete(HttpServletRequest  request,HttpServletResponse response,String orderId) throws IOException {

          PrintWriter out=response.getWriter();
         /* String id[];
          if(tt.length()==2){
              id=tt.split(":");
          }else{
              id=tt.substring(0,tt.length()-1).split(":");
          }
          for(int i=0;i<id.length;i++){
              orderservice.deleteById(Long.valueOf(id[i]));
          }*/
          orderservice.deleteById(Long.valueOf(orderId));
          out.print("ok");
      }
    @RequestMapping("/deleteorderId")
    public  String  deleteorderId(String orderId){

          String id[];
          if(orderId.length()==2){
              id=orderId.split(":");
          }else{
              id=orderId.substring(0,orderId.length()-1).split(":");
          }
          for(int i=0;i<id.length;i++){
              orderservice.deleteById(Long.valueOf(id[i]));
          }
          return "redirect:inquiry";
      }
      }

